package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    //定义一个跳转的页面
    @RequestMapping("")
    public String index(){
        return "permission";
    }


    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo){
        return permissionService.query(qo);
    }

    //重新加载权限
    @RequestMapping("reload")
    @ResponseBody
    public JsonResult reload(){
        JsonResult jsonResult = new JsonResult();

        try {
            permissionService.reload();
        }catch (Exception e){
            e.printStackTrace();

            jsonResult.setMsg("系统异常啦,我们正在殴打程序员");
        }
        return jsonResult;
    }



    @RequestMapping("listAll")
    @ResponseBody
    /*查询出所有的list的来做显示*/
    public List<Permission> listAll(){
        //查询所有的权限表达式,然后共享
        List<Permission> permissions = permissionService.selectAll();
        return permissions;
    }
    @RequestMapping("queryByRoleId")
    @ResponseBody
    /*查询出所有的list的来做显示*/
    public PageResult queryByRoleId(Long roleId){
        //查询所有的权限表达式,然后共享
        List<Permission> role = permissionService.queryByRoleId(roleId);
        return new PageResult(0,role);
    }




    @RequestMapping("listIdsByRoleId")
    @ResponseBody
    ///根据角色的id,查询所有的权限的集合
    public PageResult listIdsByRoleId(Long roleId){
        //查询所有的权限表达式,然后共享
        List<Permission> role = permissionService.queryByRoleId(roleId);
        return new PageResult(0,role);
    }



    @RequestMapping("insertPermissionsId")
    @ResponseBody
    /*查询出所有的list的来做显示*/
    public JsonResult insertPermissionsId(Role role){
        //查询所有的权限表达式,然后共享
        JsonResult jsonResult = new JsonResult();
        try {
            permissionService.insertPermissionsId(role);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult  = new JsonResult("插入权限失败.");
        }
        return jsonResult;
    }
    @RequestMapping("selectPermissionByRoleId")
    @ResponseBody
    /*查询出所有的list的来做显示*/
    public List<Long> selectPermissionByRoleId(Long id){
        //查询所有的权限表达式,然后共享
        List<Long> roleIds = null;
        try {
          roleIds =  permissionService.selectPermissionByRoleId(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return roleIds;
    }
}
